package com.nanhai.buddhist.college.module.study.controller.student.attendance.move;

import com.nanhai.buddhist.college.framework.common.pojo.CommonResult;
import com.nanhai.buddhist.college.framework.common.pojo.PageResult;
import com.nanhai.buddhist.college.module.collegebase.api.teacher.TeacherApi;
import com.nanhai.buddhist.college.module.collegebase.api.teacher.dto.TeacherRespDTO;
import com.nanhai.buddhist.college.module.study.controller.admin.attendanceleavereason.vo.AttendanceLeaveReasonRespVO;
import com.nanhai.buddhist.college.module.study.controller.admin.attendancemanagement.vo.AttendanceManagementListReqVO;
import com.nanhai.buddhist.college.module.study.controller.admin.attendancemanagement.vo.AttendanceManagementRespVO;
import com.nanhai.buddhist.college.module.study.controller.admin.classclockrecord.vo.ClockRecordCreateReqVO;
import com.nanhai.buddhist.college.module.study.controller.admin.classclockrecord.vo.ClockRecordRespVO;
import com.nanhai.buddhist.college.module.study.controller.admin.classclockrecord.vo.ClockRecordTodayReqVO;
import com.nanhai.buddhist.college.module.study.controller.admin.classclockrecord.vo.ScanCodeClockReqVO;
import com.nanhai.buddhist.college.module.study.controller.admin.timetable.vo.ClassTimetableRespVO;
import com.nanhai.buddhist.college.module.study.controller.admin.timetable.vo.ClassTimetableTodayReqVO;
import com.nanhai.buddhist.college.module.study.controller.student.attendance.vo.StudentClockRecordPageReqVO;
import com.nanhai.buddhist.college.module.study.controller.student.attendance.vo.StudentManagementPageReqVO;
import com.nanhai.buddhist.college.module.study.controller.teacher.vo.AttendanceManagementAddReq;
import com.nanhai.buddhist.college.module.study.convert.attendanceleavereason.AttendanceLeaveReasonConvert;
import com.nanhai.buddhist.college.module.study.convert.attendancemanagement.AttendanceManagementConvert;
import com.nanhai.buddhist.college.module.study.convert.classclockrecord.ClockRecordConvert;
import com.nanhai.buddhist.college.module.study.dal.dataobject.attendanceleavereason.AttendanceLeaveReasonDO;
import com.nanhai.buddhist.college.module.study.dal.dataobject.attendancemanagement.AttendanceManagementDO;
import com.nanhai.buddhist.college.module.study.dal.dataobject.classclockrecord.ClockRecordDO;
import com.nanhai.buddhist.college.module.study.dal.dataobject.timetable.ClassTimetableDO;
import com.nanhai.buddhist.college.module.study.enums.ClockRoleTypeEnum;
import com.nanhai.buddhist.college.module.study.enums.ClockTypeEnum;
import com.nanhai.buddhist.college.module.study.service.attendanceleavereason.AttendanceLeaveReasonService;
import com.nanhai.buddhist.college.module.study.service.attendancemanagement.AttendanceManagementService;
import com.nanhai.buddhist.college.module.study.service.classclockrecord.ClockRecordService;
import com.nanhai.buddhist.college.module.study.service.timetable.ClassTimetableService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

import java.util.ArrayList;
import java.util.List;

import static com.nanhai.buddhist.college.framework.common.pojo.CommonResult.success;

/**
 * @author AKai
 * @date 2022/8/28
 */
@Api(tags = "学生移动端- 学生考勤模块")
@RestController
@RequestMapping("/study/student/attendance/move")
@Validated
public class StudentAttendanceMoveController {

    @Resource
    private AttendanceManagementService attendanceManagementService;

    @Resource
    private AttendanceLeaveReasonService attendanceLeaveReasonService;

    @Resource
    private ClockRecordService clockRecordService;

    @Resource
    private ClassTimetableService classTimetableService;

    @Resource
    private TeacherApi teacherApi;

    @PostMapping("/apply/leave")
    @ApiOperation("请假申请")
    public CommonResult<String> createAttendanceManagement(@Valid @RequestBody AttendanceManagementAddReq createReqVO) {
        createReqVO.setUserType(ClockRoleTypeEnum.STUDENT.getRole());
        return success(attendanceManagementService.createAttendanceManagement(createReqVO));
    }

    @PostMapping("/clock/create")
    @ApiOperation("创建打卡记录")
    public CommonResult<String> createClockRecord(@Valid @RequestBody ClockRecordCreateReqVO createReqVO) {
        return success(clockRecordService.createClockRecord(createReqVO));
    }

    @GetMapping("/page")
    @ApiOperation("获取请假分页")
    public CommonResult<PageResult<AttendanceManagementRespVO>> getAttendanceManagementPage(@Valid StudentManagementPageReqVO pageVO) {
        pageVO.setUserType(ClockRoleTypeEnum.STUDENT.getRole());
        PageResult<AttendanceManagementDO> pageResult = attendanceManagementService.getAttendanceManagementPage(pageVO);
        return success(AttendanceManagementConvert.INSTANCE.convertPage(pageResult));
    }


    @GetMapping("/list")
    @ApiOperation("获取请假列表")
    public CommonResult<List<AttendanceManagementRespVO>> getAttendanceManagementListByQue(@Validated AttendanceManagementListReqVO reqVO) {
        reqVO.setUserType(ClockRoleTypeEnum.STUDENT.getRole()+"");
        List<AttendanceManagementDO> list = attendanceManagementService.getAttendanceManagementListByQue(reqVO);
        return success(AttendanceManagementConvert.INSTANCE.convertList(list));
    }


    @GetMapping("/studentClockPage")
    @ApiOperation("获得打卡记录分页")
    public CommonResult<PageResult<ClockRecordRespVO>> getStudentClockRecordPage(@Validated StudentClockRecordPageReqVO pageVO) {
        PageResult<ClockRecordDO> pageResult = clockRecordService.getStudentClockRecordPage(pageVO);
        return success(ClockRecordConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/getLeaveInfo")
    @ApiOperation("获得请假详情")
    public CommonResult<AttendanceManagementRespVO> getAttendanceManagement(@RequestParam("id") String id) {
        AttendanceManagementDO attendanceManagement = attendanceManagementService.getAttendanceManagement(id);
        return success(AttendanceManagementConvert.INSTANCE.convert(attendanceManagement));
    }

    @GetMapping("/getTodayClock")
    @ApiOperation("获取今日课程打卡详情")
    public CommonResult<List<ClockRecordRespVO>> getTodayClock(@Validated ClockRecordTodayReqVO recordTodayReqVO) {
        List<ClockRecordDO> todayClock = clockRecordService.getTodayClock(recordTodayReqVO);
        return success(ClockRecordConvert.INSTANCE.convertList(todayClock));
    }

    @GetMapping("/getLeaveReason")
    @ApiOperation("获取请假事由表")
    public CommonResult<List<AttendanceLeaveReasonRespVO>> getLeaveReasonAllList() {
        List<AttendanceLeaveReasonDO> list = attendanceLeaveReasonService.getAttendanceLeaveReasonAllList();
        return success(AttendanceLeaveReasonConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/studentSubmitList")
    @ApiOperation("学生提交人列表")
    public  CommonResult<List<TeacherRespDTO>> getStudentSubmitListByClassId(@RequestParam("studentId") String classesId) {
         List<TeacherRespDTO> checkedData = teacherApi.getTeacherListByStudentId(classesId).getData();
        return CommonResult.success(checkedData);
    }

    @GetMapping("/getTodaySubject")
    @ApiOperation("获取今日课程")
    public CommonResult<List<ClassTimetableRespVO>> getTodaySubjectByDate(@Validated ClassTimetableTodayReqVO reqVO) {
        List<ClassTimetableDO> list = classTimetableService.getTodaySubjectByDate(reqVO);
        return success(classTimetableService.coverNames(list));
    }

    @PostMapping("/scanCodeClock")
    @ApiOperation("扫码打卡")
    public CommonResult<String> scanCodeClock(@Valid @RequestBody ScanCodeClockReqVO createReqVO) {
        createReqVO.setClockType(ClockTypeEnum.SCAN.getType());
        createReqVO.setClockRole(ClockRoleTypeEnum.STUDENT.getRole());
        return success(clockRecordService.scanCodeClock(createReqVO));
    }

    @GetMapping("/getClassTimetableInfo")
    @ApiOperation("获得课程")
    public CommonResult<ClassTimetableRespVO> getClassTimetable(@RequestParam("id") Long id) {
        ClassTimetableDO classTimetable = classTimetableService.getClassTimetable(id);
        List<ClassTimetableDO> list = new ArrayList<>(2);
        list.add(classTimetable);
        return success(classTimetableService.coverNames(list).get(0));
    }



}
